package view
{
	import com.adobe.serialization.json.JSON;
	import com.bit101.components.CheckBox;
	import com.bit101.components.HBox;
	import com.bit101.components.ImageCheckBox;
	import com.bit101.components.ImageRadioBtn;
	import com.bit101.components.Text;
	
	import events.AddTxtEvent;
	import events.BitmapEvent;
	import events.DataObjEvent;
	import events.EditEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Matrix;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.ui.Mouse;
	
	import models.ModelLocator;
	
	public class EditItem extends HBox
	{
		[Embed(source="assets/components/SelectBg.png")]
		private var selectBg:Class;
		[Embed(source="assets/components/SelectBtn.png")]
		private var selectBtn:Class;
		private var file:FileReference = new FileReference();
		public var radio:ImageCheckBox;
		private var stateTxt:TextField;
		private var addTxtsprite:Sprite;
		private var addFileSprite:Sprite;
		private var addSaveSprite:Sprite;
		private var _data:Object;
		private var textSave:TextField;
		public var showAble:Boolean;
		public var uploadUrl:String;
		public var fileNmae:String;
		public var scale:Number;
		public var bigImgUrl:String;
		public var smaImgUrl:String;
		public function EditItem(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
		}
		public function set souce(obj:Object):void{
			this.data = obj;
			scale = this.data.w/this.data.orgW;
			radio = new ImageCheckBox(this,0,3,"",onSelect);
			radio.setEmbedBitmap(selectBg,selectBtn);
			
			addName();
			
			addTxt();
			
			addFile();
			
			stateTxt = new TextField()
			stateTxt.width = 150;
			stateTxt.height = 22;
			stateTxt.htmlText = "<font color='#666666'>        正在编辑...</font>"; 
			stateTxt.selectable = false;
			stateTxt.visible = false;
			this.addChild(stateTxt);
			
			//addSave();
			
			addProgeress();
			
			this.addEventListener(MouseEvent.CLICK,onClick);
		}
		private var uploadProgerss:TextField;
		private function addProgeress():void{
			uploadProgerss = new TextField;
			uploadProgerss.height = 20;
			uploadProgerss.autoSize = TextFieldAutoSize.LEFT;
			//uploadProgerss.htmlText = "<font color='#666666' size='16'><a href='event:myevent'>" + data.name + "</a></font>"; 
			this.addChild(uploadProgerss);
		}
		private function addName():void{
			var txt:TextField = new TextField;
			txt.height = 20;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.htmlText = "<font color='#666666' size='16'><a href='event:myevent'>" + data.name + "</a></font>"; 
			txt.addEventListener(MouseEvent.CLICK,onShowThis);
			this.addChild(txt);
		}
		private function onShowThis(event:MouseEvent):void{
			dispatch(-1);
		}
		public function onSelect(event:Event = null):void{
			var evt:DataObjEvent = new DataObjEvent(DataObjEvent.DATAEVENT);
			evt.data = data;
			evt.add = radio.selected;
			this.parent.dispatchEvent(evt);
			
			dispatch(int(radio.selected));
		}
		public function add():void{
			var evt:DataObjEvent = new DataObjEvent(DataObjEvent.DATAEVENT);
			evt.data = data;
			evt.add = radio.selected;
			this.parent.dispatchEvent(evt);
		}
		public function set state(value:Boolean):void{
			stateTxt.visible = value;
		}
		public function onClick(event:MouseEvent=null):void{
			//radio.selected = true;
			if(ListItem(this.parent).currentEdit){
				ListItem(this.parent).currentEdit.state = false;
			}
			ListItem(this.parent).currentEdit = this;
			this.state = true;
			
		}
		public function dispatch(type:int):void{
			var evt:EditEvent = new EditEvent(EditEvent.EDITEVENT);
			evt.data = data;
			evt.showType = type;
			this.parent.dispatchEvent(evt);
		}
		private function addTxt():void{
			addTxtsprite = new Sprite;
			var txt:TextField = new TextField()
			txt.width = 54;
			txt.height = 22;
			txt.htmlText = "<a href='event:myText'><p align='center'><font color='#666666'>添文字</font></p></a>";
			txt.addEventListener(MouseEvent.CLICK,onTxtClick);
			addTxtsprite.graphics.beginFill(0xd3d3d3);
			addTxtsprite.graphics.drawRoundRect(0,0,54,20,4,4);
			addTxtsprite.graphics.endFill();
			this.addChild(addTxtsprite);
			addTxtsprite.addChild(txt);
		}
		public function onTxtClick(event:MouseEvent = null):void{
			//dispatch(1);
			this.parent.dispatchEvent(new AddTxtEvent(AddTxtEvent.ADDTXTEVENT));
			radio.selected = true;
			onSelect();
		}
		private var uploadTxt:TextField;
		private function addFile():void{
			addFileSprite = new Sprite;
			uploadTxt = new TextField()
			uploadTxt.width = 60;
			uploadTxt.height = 22;
			uploadTxt.htmlText = "<a href='event:myText'><p align='center'><font color='#666666'>上传图片</font></p></a>";
			addFileSprite.graphics.beginFill(0xd3d3d3);
			addFileSprite.graphics.drawRoundRect(0,0,60,20,4,4);
			addFileSprite.graphics.endFill();
			this.addChild(addFileSprite);
			addFileSprite.addChild(uploadTxt);
			uploadTxt.addEventListener(MouseEvent.CLICK,getFile);
		}
		private function addSave():void{
			addSaveSprite = new Sprite;
			textSave = new TextField()
			textSave.width = 40;
			textSave.height = 22;
			textSave.htmlText = "<a href='event:myText'><p align='center'><font color='#666666'>保存</font></p></a>";
			addSaveSprite.graphics.beginFill(0xd3d3d3);
			addSaveSprite.graphics.drawRoundRect(0,0,40,20,4,4);
			addSaveSprite.graphics.endFill();
			this.addChild(addSaveSprite);
			addSaveSprite.addChild(textSave);
			textSave.addEventListener(MouseEvent.CLICK,save);
		}
		private function save(event:MouseEvent):void{
			if(textSave.text == "保存"){
				addTxtsprite.visible = false;
				addFileSprite.visible = false;
				textSave.htmlText = "<a href='event:myText'><p align='center'><font color='#666666'>修改</font></p></a>";
			}else{
				addTxtsprite.visible = true;
				addFileSprite.visible = true;
				textSave.htmlText = "<a href='event:myText'><p align='center'><font color='#666666'>保存</font></p></a>";
			}
			/*var evt:DataObjEvent = new DataObjEvent(DataObjEvent.DATAEVENT);
			evt.data = data;
			this.parent.dispatchEvent(evt);*/
		}
		public function getFile(event:MouseEvent = null):void{
			radio.selected = true;
			onSelect();
			file.browse([new FileFilter("图片", "*.jpg;*.gif;*.png")]);
			file.addEventListener(Event.SELECT,onFileSelect);
		}
		private function onFileSelect(event:Event):void{
			uploadTxt.htmlText = "<a href='event:myText'><p align='center'><font color='#666666'>重新上传</font></p></a>";
			//file.load();
			this.uploadUrl = "file:///D:/png/" + file.name;
			fileNmae = file.name;
			//file.addEventListener(Event.COMPLETE,onComplete);
			file.removeEventListener(Event.SELECT,getFile);
			sendServer();
			//moniServer();
		}
		private function moniServer():void{
			var str:String = uploadUrl.substr(0,uploadUrl.length-4) + "_sma.jpg";
			var evt:DataEvent = new DataEvent(DataEvent.UPLOAD_COMPLETE_DATA)
			evt.data = str;
			onUploadComplete(evt);
		}
		private function sendServer():void{
			var url:String = ModelLocator.getInstance().submit + "&scale=" + this.scale;
			
			var urlreq:URLRequest = new URLRequest(url)
			//urlreq.method = URLRequestMethod.POST;  
			//urlreq.contentType = "application/octet-stream";  
			file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadComplete);
			file.addEventListener(ProgressEvent.PROGRESS,onProgress);
			file.upload(urlreq);
		}
		private function onUploadComplete(event:DataEvent):void{
			if(String(event.data) == "0"){
				trace("图片太大");
				return;
			}
			var obj:Object = JSON.decode(String(event.data));
			this.bigImgUrl = obj[0];
			this.smaImgUrl = obj[1];
			var urlreq:URLRequest = new URLRequest(ModelLocator.getInstance().root + smaImgUrl);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadServer);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgress);
			loader.load(urlreq);
			
		}
		private function onProgress(event:ProgressEvent):void{
			var str:String = int(event.bytesLoaded/event.bytesTotal*100) + "%";
			uploadProgerss.htmlText = "<font color='#666666'>" + str + "</font>"; 
		}
		private function onLoadServer(e:Event):void{
			onSelect();
			
			var bitmap:Bitmap = e.target.content as Bitmap
			var bitmapdata:BitmapData = new BitmapData(bitmap.width/this.scale,bitmap.height/this.scale,true,0);
			var ma:Matrix = new Matrix();
			ma.scale(1/this.scale,1/this.scale);
			bitmapdata.draw(bitmap,ma);
			
			var evt:BitmapEvent = new BitmapEvent(BitmapEvent.BITMAPEVENT);
			evt.bitmap = new Bitmap(bitmapdata);
			evt.levle = 0;
			evt.url = this;
			evt.scale = this.scale;
			this.parent.dispatchEvent(evt);
		} 
		
		private var loader:Loader = new Loader;
		private function onComplete(e:Event):void
		{
			file.removeEventListener(Event.COMPLETE,onComplete);
			loader.loadBytes(file.data);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
		}
		private function onLoadComplete(e:Event):void
		{
			var evt:BitmapEvent = new BitmapEvent(BitmapEvent.BITMAPEVENT);
			evt.bitmap = e.target.content as Bitmap;
			evt.levle = 0;
			evt.url = this;
			evt.scale = this.scale;
			//trace(evt.bitmap.width*this.scale,evt.bitmap.height*scale)
			this.parent.dispatchEvent(evt);
		} 

		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
		}

		
	}
}
